package cn.wqb.bpp.common.utils;

import org.springframework.util.StringUtils;

public class PrivacyUtils {

    /**
     * 内容部分变星号（支持账号邮箱银行卡号自动判断）
     *
     * @param content
     * @return
     */
    public static String partialToStars(String content) {
        if (StringUtils.isEmpty(content)) {
            return content;
        }
        if (isBankCardNumber(content)) {
            return content.substring(0, 6) + "******" + content.substring(content.length() - 4, content.length());
        } else if (isPhoneNumber(content)) {
            return content.substring(0, 3) + "****" + content.substring(content.length()-4, content.length());
        } else if (isEmailNumber(content)) {
            String[] arr = content.split("\\@");
            return content.substring(0, 3) + "***@" + arr[1];
        } else {
            if (content.length() == 1) {
                return "*";
            } else if (content.length() == 2) {
                return content.substring(0, 1) + "*";
            } else if (content.length() == 3) {
                return content.substring(0, 2) + "*";
            } else {
                return content.substring(0, 3) + "***";
            }
        }
    }

    public static boolean isBankCardNumber(String content) {
        boolean num16 = content.matches("^\\d{16}$");
        boolean num17 = content.matches("^\\d{17}$");
        boolean num18 = content.matches("^\\d{18}$");
        boolean num19 = content.matches("^\\d{19}$");
        return num16 || num17 || num18 || num19;
    }

    public static boolean isPhoneNumber(String content) {
        String regex = "^[1][3578][0-9]{9}$";
        return content.matches(regex);
    }

    public static boolean isEmailNumber(String content) {
        String regex = "[a-zA-Z0-9_]{6,12}+@[a-zA-Z]+(\\.[a-zA-Z]+){1,3}";
        return content.matches(regex);
    }

}
